package xian.woniuxy.be.web;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xian.woniuxy.be.entity.ResultVO;
import xian.woniuxy.be.entity.Users;

/**
 * @author ljy
 * @time 2022/01/03 15:02:53
 */
@RestController
public class AuthController {
    @RequestMapping("auth/login")
    public ResultVO login(@RequestBody Users user){
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(), user.getPassword());
        try {
            subject.login(token);
            return ResultVO.success("认证成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVO.failure("认证失败");
        }
    }

    @RequestMapping("auth/logout")
    public ResultVO logout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return ResultVO.success();
    }
}
